﻿using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.GameState;
using TaleWorlds.CampaignSystem.Map;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;
using TaleWorlds.SaveSystem.Load;

namespace TaleWorlds.CampaignSystem.Siege
{
	// Token: 0x02000283 RID: 643
	public class BesiegerCamp : ISiegeEventSide
	{
		// Token: 0x06002270 RID: 8816 RVA: 0x00092968 File Offset: 0x00090B68
		internal static void AutoGeneratedStaticCollectObjectsBesiegerCamp(object o, List<object> collectedObjects)
		{
			((BesiegerCamp)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x06002271 RID: 8817 RVA: 0x00092978 File Offset: 0x00090B78
		protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			collectedObjects.Add(this._besiegerParties);
			collectedObjects.Add(this._leaderParty);
			collectedObjects.Add(this._siegeEngineMissiles);
			collectedObjects.Add(this.SiegeEvent);
			collectedObjects.Add(this.SiegeEngines);
			collectedObjects.Add(this.SiegeStrategy);
		}

		// Token: 0x06002272 RID: 8818 RVA: 0x000929CD File Offset: 0x00090BCD
		internal static object AutoGeneratedGetMemberValueSiegeEvent(object o)
		{
			return ((BesiegerCamp)o).SiegeEvent;
		}

		// Token: 0x06002273 RID: 8819 RVA: 0x000929DA File Offset: 0x00090BDA
		internal static object AutoGeneratedGetMemberValueSiegeEngines(object o)
		{
			return ((BesiegerCamp)o).SiegeEngines;
		}

		// Token: 0x06002274 RID: 8820 RVA: 0x000929E7 File Offset: 0x00090BE7
		internal static object AutoGeneratedGetMemberValueSiegeStrategy(object o)
		{
			return ((BesiegerCamp)o).SiegeStrategy;
		}

		// Token: 0x06002275 RID: 8821 RVA: 0x000929F4 File Offset: 0x00090BF4
		internal static object AutoGeneratedGetMemberValueNumberOfTroopsKilledOnSide(object o)
		{
			return ((BesiegerCamp)o).NumberOfTroopsKilledOnSide;
		}

		// Token: 0x06002276 RID: 8822 RVA: 0x00092A06 File Offset: 0x00090C06
		internal static object AutoGeneratedGetMemberValue_besiegerParties(object o)
		{
			return ((BesiegerCamp)o)._besiegerParties;
		}

		// Token: 0x06002277 RID: 8823 RVA: 0x00092A13 File Offset: 0x00090C13
		internal static object AutoGeneratedGetMemberValue_leaderParty(object o)
		{
			return ((BesiegerCamp)o)._leaderParty;
		}

		// Token: 0x06002278 RID: 8824 RVA: 0x00092A20 File Offset: 0x00090C20
		internal static object AutoGeneratedGetMemberValue_siegeEngineMissiles(object o)
		{
			return ((BesiegerCamp)o)._siegeEngineMissiles;
		}

		// Token: 0x1700089D RID: 2205
		// (get) Token: 0x06002279 RID: 8825 RVA: 0x00092A2D File Offset: 0x00090C2D
		// (set) Token: 0x0600227A RID: 8826 RVA: 0x00092A35 File Offset: 0x00090C35
		[SaveableProperty(6)]
		public SiegeEvent SiegeEvent { get; private set; }

		// Token: 0x1700089E RID: 2206
		// (get) Token: 0x0600227B RID: 8827 RVA: 0x00092A3E File Offset: 0x00090C3E
		// (set) Token: 0x0600227C RID: 8828 RVA: 0x00092A46 File Offset: 0x00090C46
		[SaveableProperty(7)]
		public SiegeEvent.SiegeEnginesContainer SiegeEngines { get; private set; }

		// Token: 0x1700089F RID: 2207
		// (get) Token: 0x0600227D RID: 8829 RVA: 0x00092A4F File Offset: 0x00090C4F
		public MobileParty LeaderParty
		{
			get
			{
				return this._leaderParty;
			}
		}

		// Token: 0x0600227E RID: 8830 RVA: 0x00092A57 File Offset: 0x00090C57
		public IEnumerable<PartyBase> GetInvolvedPartiesForEventType(MapEvent.BattleTypes mapEventType = MapEvent.BattleTypes.Siege)
		{
			foreach (MobileParty mobileParty in this._besiegerParties)
			{
				yield return mobileParty.Party;
			}
			List<MobileParty>.Enumerator enumerator = default(List<MobileParty>.Enumerator);
			yield break;
			yield break;
		}

		// Token: 0x0600227F RID: 8831 RVA: 0x00092A67 File Offset: 0x00090C67
		public PartyBase GetNextInvolvedPartyForEventType(ref int partyIndex, MapEvent.BattleTypes mapEventType = MapEvent.BattleTypes.Siege)
		{
			partyIndex++;
			if (partyIndex >= this._besiegerParties.Count)
			{
				return null;
			}
			return this._besiegerParties[partyIndex].Party;
		}

		// Token: 0x06002280 RID: 8832 RVA: 0x00092A94 File Offset: 0x00090C94
		public bool HasInvolvedPartyForEventType(PartyBase party, MapEvent.BattleTypes mapEventType = MapEvent.BattleTypes.Siege)
		{
			using (List<MobileParty>.Enumerator enumerator = this._besiegerParties.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Party == party)
					{
						return true;
					}
				}
			}
			return false;
		}

		// Token: 0x170008A0 RID: 2208
		// (get) Token: 0x06002281 RID: 8833 RVA: 0x00092AF0 File Offset: 0x00090CF0
		public BattleSideEnum BattleSide
		{
			get
			{
				return BattleSideEnum.Attacker;
			}
		}

		// Token: 0x170008A1 RID: 2209
		// (get) Token: 0x06002282 RID: 8834 RVA: 0x00092AF3 File Offset: 0x00090CF3
		public MBReadOnlyList<SiegeEvent.SiegeEngineMissile> SiegeEngineMissiles
		{
			get
			{
				return this._siegeEngineMissiles;
			}
		}

		// Token: 0x170008A2 RID: 2210
		// (get) Token: 0x06002283 RID: 8835 RVA: 0x00092AFB File Offset: 0x00090CFB
		// (set) Token: 0x06002284 RID: 8836 RVA: 0x00092B03 File Offset: 0x00090D03
		[SaveableProperty(10)]
		public SiegeStrategy SiegeStrategy { get; private set; }

		// Token: 0x170008A3 RID: 2211
		// (get) Token: 0x06002285 RID: 8837 RVA: 0x00092B0C File Offset: 0x00090D0C
		// (set) Token: 0x06002286 RID: 8838 RVA: 0x00092B14 File Offset: 0x00090D14
		[SaveableProperty(11)]
		public int NumberOfTroopsKilledOnSide { get; private set; }

		// Token: 0x06002287 RID: 8839 RVA: 0x00092B1D File Offset: 0x00090D1D
		public BesiegerCamp(SiegeEvent siegeEvent)
		{
			this._besiegerParties = new MBList<MobileParty>();
			this.SiegeEvent = siegeEvent;
		}

		// Token: 0x06002288 RID: 8840 RVA: 0x00092B38 File Offset: 0x00090D38
		public bool IsBesiegerSideParty(MobileParty mobileParty)
		{
			Func<MobileParty, bool> <>9__1;
			return this.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege).Any(delegate(PartyBase t)
			{
				if (t != mobileParty.Party)
				{
					IEnumerable<MobileParty> attachedParties = t.MobileParty.AttachedParties;
					Func<MobileParty, bool> predicate;
					if ((predicate = <>9__1) == null)
					{
						predicate = (<>9__1 = ((MobileParty k) => k == mobileParty));
					}
					return attachedParties.Any(predicate);
				}
				return true;
			});
		}

		// Token: 0x170008A4 RID: 2212
		// (get) Token: 0x06002289 RID: 8841 RVA: 0x00092B6C File Offset: 0x00090D6C
		private float PreparationProgress
		{
			get
			{
				SiegeEvent.SiegeEnginesContainer siegeEngines = this.SiegeEngines;
				float? num;
				if (siegeEngines == null)
				{
					num = null;
				}
				else
				{
					SiegeEvent.SiegeEngineConstructionProgress siegePreparations = siegeEngines.SiegePreparations;
					num = ((siegePreparations != null) ? new float?(siegePreparations.Progress) : null);
				}
				float? num2 = num;
				if (num2 == null)
				{
					return 0f;
				}
				return num2.GetValueOrDefault();
			}
		}

		// Token: 0x170008A5 RID: 2213
		// (get) Token: 0x0600228A RID: 8842 RVA: 0x00092BC3 File Offset: 0x00090DC3
		public bool IsPreparationComplete
		{
			get
			{
				return this.PreparationProgress >= 1f;
			}
		}

		// Token: 0x170008A6 RID: 2214
		// (get) Token: 0x0600228B RID: 8843 RVA: 0x00092BD5 File Offset: 0x00090DD5
		public bool IsReadyToBesiege
		{
			get
			{
				return this.IsPreparationComplete && this.StartingAssaultOnBesiegedSettlementIsLogical();
			}
		}

		// Token: 0x0600228C RID: 8844 RVA: 0x00092BE8 File Offset: 0x00090DE8
		public void InitializeSiegeEventSide()
		{
			this.SiegeStrategy = DefaultSiegeStrategies.Custom;
			this.NumberOfTroopsKilledOnSide = 0;
			SiegeEvent.SiegeEngineConstructionProgress siegePreparations = new SiegeEvent.SiegeEngineConstructionProgress(DefaultSiegeEngineTypes.Preparations, this.PreparationProgress, (float)DefaultSiegeEngineTypes.Preparations.BaseHitPoints);
			this.SiegeEngines = new SiegeEvent.SiegeEnginesContainer(BattleSideEnum.Attacker, siegePreparations);
			this._siegeEngineMissiles = new MBList<SiegeEvent.SiegeEngineMissile>();
			this.SetPrebuiltSiegeEngines();
		}

		// Token: 0x0600228D RID: 8845 RVA: 0x00092C43 File Offset: 0x00090E43
		public void OnTroopsKilledOnSide(int killCount)
		{
			this.NumberOfTroopsKilledOnSide += killCount;
		}

		// Token: 0x0600228E RID: 8846 RVA: 0x00092C53 File Offset: 0x00090E53
		public void SetSiegeStrategy(SiegeStrategy strategy)
		{
			this.SiegeStrategy = strategy;
		}

		// Token: 0x0600228F RID: 8847 RVA: 0x00092C5C File Offset: 0x00090E5C
		internal void AddSiegePartyInternal(MobileParty mobileParty)
		{
			this._besiegerParties.Add(mobileParty);
			if (this._besiegerParties.Count == 1)
			{
				this.SiegeEvent.BesiegedSettlement.LastAttackerParty = mobileParty;
			}
			this._leaderParty = Campaign.Current.Models.EncounterModel.GetLeaderOfSiegeEvent(this.SiegeEvent, BattleSideEnum.Attacker).PartyBelongedTo;
			this.ChangeSiegeStrategyIfNeeded();
		}

		// Token: 0x06002290 RID: 8848 RVA: 0x00092CC0 File Offset: 0x00090EC0
		internal void RemoveSiegePartyInternal(MobileParty mobileParty)
		{
			this._besiegerParties.Remove(mobileParty);
			if (mobileParty != null)
			{
				this.OnPartyLeftSiege(mobileParty);
			}
			if (this._besiegerParties.Count == 0)
			{
				this.SiegeEvent.FinalizeSiegeEvent();
				return;
			}
			if (this._leaderParty == mobileParty && (this._leaderParty == null || this._leaderParty.MapEvent == null || this._leaderParty.MapEvent.State != MapEventState.WaitingRemoval))
			{
				Hero leaderOfSiegeEvent = Campaign.Current.Models.EncounterModel.GetLeaderOfSiegeEvent(this.SiegeEvent, BattleSideEnum.Attacker);
				this._leaderParty = ((leaderOfSiegeEvent != null) ? leaderOfSiegeEvent.PartyBelongedTo : null);
				this.ChangeSiegeStrategyIfNeeded();
			}
		}

		// Token: 0x06002291 RID: 8849 RVA: 0x00092D64 File Offset: 0x00090F64
		public void RemoveAllSiegeParties()
		{
			MapEvent mapEvent = this.SiegeEvent.BesiegedSettlement.Party.MapEvent;
			if (mapEvent != null && mapEvent.IsSiegeAssault && !mapEvent.IsFinished)
			{
				Debug.FailedAssert("RemoveAllParties called before mapEvent is cleared", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\Siege\\BesiegerCamp.cs", "RemoveAllSiegeParties", 168);
			}
			while (this._besiegerParties.Count > 0)
			{
				this._besiegerParties[0].BesiegerCamp = null;
			}
		}

		// Token: 0x06002292 RID: 8850 RVA: 0x00092DD8 File Offset: 0x00090FD8
		private void OnPartyLeftSiege(MobileParty mobileParty)
		{
			if (mobileParty == MobileParty.MainParty && PlayerSiege.PlayerSiegeEvent != null)
			{
				PlayerSiege.ClosePlayerSiege();
			}
			if (Campaign.Current.Models.PartyImpairmentModel.CanGetDisorganized(mobileParty.Party))
			{
				mobileParty.SetDisorganized(true);
			}
			if (this._besiegerParties.Contains(MobileParty.MainParty) && (MobileParty.MainParty.MapEvent == null || !MobileParty.MainParty.MapEvent.HasWinner))
			{
				Campaign.Current.TimeControlMode = CampaignTimeControlMode.Stop;
				MenuContext currentMenuContext = Campaign.Current.CurrentMenuContext;
				if (currentMenuContext != null)
				{
					currentMenuContext.Refresh();
				}
			}
			mobileParty.Ai.SetMoveModeHold();
		}

		// Token: 0x06002293 RID: 8851 RVA: 0x00092E78 File Offset: 0x00091078
		private void ChangeSiegeStrategyIfNeeded()
		{
			if (this.SiegeStrategy == DefaultSiegeStrategies.Custom && this._leaderParty.LeaderHero != Hero.MainHero)
			{
				SiegeStrategy siegeStrategy = null;
				float num = float.MinValue;
				foreach (SiegeStrategy siegeStrategy2 in DefaultSiegeStrategies.AllAttackerStrategies)
				{
					float num2 = Campaign.Current.Models.SiegeEventModel.GetSiegeStrategyScore(this.SiegeEvent, BattleSideEnum.Attacker, siegeStrategy2) * (0.5f + 0.5f * MBRandom.RandomFloat);
					if (num2 > num)
					{
						num = num2;
						siegeStrategy = siegeStrategy2;
					}
				}
				this.SetSiegeStrategy(siegeStrategy);
			}
		}

		// Token: 0x06002294 RID: 8852 RVA: 0x00092F28 File Offset: 0x00091128
		internal void SetSiegeCampPartyPosition(MobileParty mobileParty)
		{
			MatrixFrame[] besiegerCampPositions = this.SiegeEvent.BesiegedSettlement.Town.BesiegerCampPositions1;
			MatrixFrame[] besiegerCampPositions2 = this.SiegeEvent.BesiegedSettlement.Town.BesiegerCampPositions2;
			int num = 0;
			Vec2 vec;
			bool flag;
			do
			{
				int num2 = MBRandom.RandomInt(besiegerCampPositions.Length);
				float randomFloat = MBRandom.RandomFloat;
				float randomFloat2 = MBRandom.RandomFloat;
				vec = besiegerCampPositions[num2].origin.AsVec2 + randomFloat * besiegerCampPositions[num2].rotation.s.AsVec2 + randomFloat2 * besiegerCampPositions[num2].rotation.f.AsVec2;
				flag = false;
				foreach (PartyBase partyBase in this.SiegeEvent.BesiegerCamp.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege))
				{
					if ((vec - partyBase.MobileParty.VisualPosition2DWithoutError).LengthSquared < 0.25f)
					{
						flag = true;
						break;
					}
				}
				num++;
			}
			while (flag && num < 20);
			if (num == 20 && besiegerCampPositions2 != null && besiegerCampPositions2.Length != 0)
			{
				num = 0;
				do
				{
					int num3 = MBRandom.RandomInt(besiegerCampPositions2.Length);
					float randomFloat3 = MBRandom.RandomFloat;
					float randomFloat4 = MBRandom.RandomFloat;
					vec = besiegerCampPositions2[num3].origin.AsVec2 + randomFloat3 * besiegerCampPositions2[num3].rotation.s.AsVec2 + randomFloat4 * besiegerCampPositions2[num3].rotation.f.AsVec2;
					flag = false;
					foreach (PartyBase partyBase2 in this.SiegeEvent.BesiegerCamp.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege))
					{
						if ((vec - partyBase2.MobileParty.VisualPosition2DWithoutError).LengthSquared < 0.25f)
						{
							flag = true;
							break;
						}
					}
					num++;
				}
				while (flag && num < 20);
			}
			mobileParty.Position2D = vec;
			mobileParty.Party.SetVisualAsDirty();
		}

		// Token: 0x06002295 RID: 8853 RVA: 0x00093174 File Offset: 0x00091374
		public void AddSiegeEngineMissile(SiegeEvent.SiegeEngineMissile missile)
		{
			this._siegeEngineMissiles.Add(missile);
		}

		// Token: 0x06002296 RID: 8854 RVA: 0x00093182 File Offset: 0x00091382
		public void RemoveDeprecatedMissiles()
		{
			this._siegeEngineMissiles.RemoveAll((SiegeEvent.SiegeEngineMissile missile) => missile.CollisionTime.IsPast);
		}

		// Token: 0x06002297 RID: 8855 RVA: 0x000931B0 File Offset: 0x000913B0
		private bool StartingAssaultOnBesiegedSettlementIsLogical()
		{
			float num = 0f;
			float num2 = (MobileParty.MainParty.CurrentSettlement == this.SiegeEvent.BesiegedSettlement) ? 0.5f : 1f;
			foreach (PartyBase partyBase in this.SiegeEvent.BesiegedSettlement.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege))
			{
				if (partyBase.IsMobile && partyBase.MobileParty.CurrentSettlement == this.SiegeEvent.BesiegedSettlement && (partyBase.MobileParty.Aggressiveness > 0.01f || partyBase.MobileParty.IsMilitia || partyBase.MobileParty.IsGarrison))
				{
					num += num2 * partyBase.TotalStrength;
				}
			}
			float num3 = 0f;
			LocatableSearchData<MobileParty> locatableSearchData = Campaign.Current.MobilePartyLocator.StartFindingLocatablesAroundPosition(this.SiegeEvent.BesiegedSettlement.Position2D, 10f);
			for (MobileParty mobileParty = Campaign.Current.MobilePartyLocator.FindNextLocatable(ref locatableSearchData); mobileParty != null; mobileParty = Campaign.Current.MobilePartyLocator.FindNextLocatable(ref locatableSearchData))
			{
				if (mobileParty.Aggressiveness > 0f && mobileParty.MapFaction == this.SiegeEvent.BesiegedSettlement.MapFaction && mobileParty.CurrentSettlement == null)
				{
					num3 += mobileParty.Party.TotalStrength;
				}
			}
			float num4 = 0f;
			foreach (PartyBase partyBase2 in this.SiegeEvent.BesiegerCamp.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege))
			{
				num4 += partyBase2.TotalStrength;
			}
			float settlementAdvantage = Campaign.Current.Models.CombatSimulationModel.GetSettlementAdvantage(this.SiegeEvent.BesiegedSettlement);
			float maximumSiegeEquipmentProgress = Campaign.Current.Models.CombatSimulationModel.GetMaximumSiegeEquipmentProgress(this.SiegeEvent.BesiegedSettlement);
			float randomFloat = MBRandom.RandomFloat;
			float num5 = num4 / (num * MathF.Pow(settlementAdvantage, 0.67f));
			bool result = false;
			float num6 = 0.9f;
			if (num5 > num6)
			{
				float num7 = 0f;
				float num8 = 0f;
				foreach (PartyBase partyBase3 in this.SiegeEvent.BesiegerCamp.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege))
				{
					num7 += partyBase3.MobileParty.Food;
					num8 += -partyBase3.MobileParty.FoodChange;
				}
				float num9 = MathF.Max(0f, num7) / num8;
				float num10 = MathF.Min(1f, num3 / num4);
				float num11 = (num9 < 3f) ? ((1f + (3f - num9) * (3f - num9)) * 0.2f) : 0f;
				float num12 = (MathF.Min(4f, MathF.Max(num5, 1f)) - 1f) * 0.2f;
				float totalStrength = this.SiegeEvent.BesiegerCamp.LeaderParty.MapFaction.TotalStrength;
				float totalStrength2 = this.SiegeEvent.BesiegedSettlement.MapFaction.TotalStrength;
				float num13 = (totalStrength < totalStrength2) ? ((1f - (totalStrength + 0.01f) / (totalStrength2 + 0.01f)) * 0.6f) : 0f;
				float y = MathF.Max(0.5f, 3f - num12 - num10 - num11 - num13);
				float num14 = MathF.Pow(settlementAdvantage, y);
				int numberOfEquipmentsBuilt = Campaign.Current.Models.CombatSimulationModel.GetNumberOfEquipmentsBuilt(this.SiegeEvent.BesiegedSettlement);
				float num15 = (MathF.Min(9f, num5) - num6) / num14;
				num15 *= ((float)MathF.Min(2, numberOfEquipmentsBuilt) + 1f) / 3f;
				num15 *= 1f - 0.66f * (maximumSiegeEquipmentProgress * maximumSiegeEquipmentProgress);
				result = (num == 0f || randomFloat < num15);
			}
			return result;
		}

		// Token: 0x06002298 RID: 8856 RVA: 0x000935D8 File Offset: 0x000917D8
		private void SetPrebuiltSiegeEngines()
		{
			foreach (SiegeEngineType siegeEngine in Campaign.Current.Models.SiegeEventModel.GetPrebuiltSiegeEnginesOfSiegeCamp(this))
			{
				float siegeEngineHitPoints = Campaign.Current.Models.SiegeEventModel.GetSiegeEngineHitPoints(this.SiegeEvent, siegeEngine, BattleSideEnum.Attacker);
				SiegeEvent.SiegeEngineConstructionProgress siegeEngineConstructionProgress = new SiegeEvent.SiegeEngineConstructionProgress(siegeEngine, 1f, siegeEngineHitPoints);
				this.SiegeEngines.AddPrebuiltEngineToReserve(siegeEngineConstructionProgress);
				this.SiegeEvent.CreateSiegeObject(siegeEngineConstructionProgress, this.SiegeEvent.GetSiegeEventSide(BattleSideEnum.Defender));
			}
		}

		// Token: 0x06002299 RID: 8857 RVA: 0x0009367C File Offset: 0x0009187C
		private float GetDistanceBetweenRangedEngineAndWall(int rangedEngine, int wallIndex)
		{
			float num = (float)rangedEngine * 1f;
			float num2 = 0.5f + 2f * (float)wallIndex;
			return MathF.Abs(num - num2) + 2f;
		}

		// Token: 0x0600229A RID: 8858 RVA: 0x000936AD File Offset: 0x000918AD
		private float PriorityCalculationForWalls(float distance)
		{
			return 5f - distance;
		}

		// Token: 0x0600229B RID: 8859 RVA: 0x000936B8 File Offset: 0x000918B8
		private void FindAttackableWallSectionWithHighestPriority(int attackerSlotIndex, SiegeEngineType siegeEngine, out int targetIndex, out float targetPriority)
		{
			targetIndex = -1;
			targetPriority = 0f;
			if (siegeEngine.IsAntiPersonnel)
			{
				return;
			}
			float num = 9999f;
			MBReadOnlyList<float> settlementWallSectionHitPointsRatioList = this.SiegeEvent.BesiegedSettlement.SettlementWallSectionHitPointsRatioList;
			for (int i = 0; i < settlementWallSectionHitPointsRatioList.Count; i++)
			{
				if (settlementWallSectionHitPointsRatioList[i] > 0f)
				{
					float distanceBetweenRangedEngineAndWall = this.GetDistanceBetweenRangedEngineAndWall(attackerSlotIndex, i);
					float num2 = this.PriorityCalculationForWalls(distanceBetweenRangedEngineAndWall);
					if (num2 > targetPriority || (MathF.Abs(num2 - targetPriority) < 0.0001f && num > distanceBetweenRangedEngineAndWall))
					{
						targetIndex = i;
						targetPriority = num2;
						num = distanceBetweenRangedEngineAndWall;
					}
				}
			}
		}

		// Token: 0x0600229C RID: 8860 RVA: 0x0009374C File Offset: 0x0009194C
		public void BombardHitWalls(SiegeEngineType attackerEngineType, int wallIndex)
		{
			MBReadOnlyList<float> settlementWallSectionHitPointsRatioList = this.SiegeEvent.BesiegedSettlement.SettlementWallSectionHitPointsRatioList;
			if (settlementWallSectionHitPointsRatioList[wallIndex] > 0f)
			{
				float num = Campaign.Current.Models.SiegeEventModel.GetSiegeEngineDamage(this.SiegeEvent, BattleSideEnum.Attacker, attackerEngineType, SiegeBombardTargets.Wall) / this.SiegeEvent.BesiegedSettlement.MaxHitPointsOfOneWallSection;
				this.SiegeEvent.BesiegedSettlement.SetWallSectionHitPointsRatioAtIndex(wallIndex, MBMath.ClampFloat(settlementWallSectionHitPointsRatioList[wallIndex] - num, 0f, 1f));
				bool flag = settlementWallSectionHitPointsRatioList[wallIndex] <= 0f;
				if (flag)
				{
					this.SiegeEvent.BesiegedSettlement.Party.SetVisualAsDirty();
				}
				CampaignEventDispatcher.Instance.OnSiegeBombardmentWallHit(this.LeaderParty, this.SiegeEvent.BesiegedSettlement, BattleSideEnum.Attacker, attackerEngineType, flag);
			}
		}

		// Token: 0x0600229D RID: 8861 RVA: 0x0009381C File Offset: 0x00091A1C
		public void GetAttackTarget(ISiegeEventSide siegeEventSide, SiegeEngineType siegeEngine, int siegeEngineSlot, out SiegeBombardTargets targetType, out int targetIndex)
		{
			targetType = SiegeBombardTargets.None;
			targetIndex = -1;
			int num;
			float num2;
			siegeEventSide.SiegeEvent.FindAttackableRangedEngineWithHighestPriority(this, siegeEngineSlot, out num, out num2);
			int num3;
			float num4;
			this.FindAttackableWallSectionWithHighestPriority(siegeEngineSlot, siegeEngine, out num3, out num4);
			if (num == -1 && num3 == -1)
			{
				return;
			}
			if (num == -1)
			{
				targetIndex = num3;
				targetType = SiegeBombardTargets.Wall;
				return;
			}
			if (num3 == -1)
			{
				targetIndex = num;
				targetType = SiegeBombardTargets.RangedEngines;
				return;
			}
			float num5 = num2 + num4;
			if (MBRandom.RandomFloat * num5 < num2)
			{
				targetIndex = num;
				targetType = SiegeBombardTargets.RangedEngines;
				return;
			}
			targetIndex = num3;
			targetType = SiegeBombardTargets.Wall;
		}

		// Token: 0x0600229E RID: 8862 RVA: 0x00093892 File Offset: 0x00091A92
		public void FinalizeSiegeEvent()
		{
			if (!this.GetInvolvedPartiesForEventType(MapEvent.BattleTypes.Siege).IsEmpty<PartyBase>())
			{
				this.RemoveAllSiegeParties();
			}
		}

		// Token: 0x0600229F RID: 8863 RVA: 0x000938A8 File Offset: 0x00091AA8
		[LateLoadInitializationCallback]
		private void OnLoad(MetaData metaData, ObjectLoadData objectLoadData)
		{
			if (this._leaderParty == null && MBSaveLoad.IsUpdatingGameVersion && MBSaveLoad.LastLoadedGameVersion < ApplicationVersion.FromString("v1.2.0", 54620))
			{
				this._leaderParty = ((this._besiegerParties.Count > 0) ? this._besiegerParties[0] : null);
			}
		}

		// Token: 0x04000A95 RID: 2709
		[SaveableField(8)]
		private readonly MBList<MobileParty> _besiegerParties;

		// Token: 0x04000A96 RID: 2710
		[SaveableField(9)]
		private MobileParty _leaderParty;

		// Token: 0x04000A97 RID: 2711
		[SaveableField(1)]
		private MBList<SiegeEvent.SiegeEngineMissile> _siegeEngineMissiles;
	}
}
